﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ResxLocalizationEditor {
	static class ResourceHelper {

		public static string[] GetFilesFromDefaultResource(string fullResourcePath) {
			var path = Path.GetDirectoryName(fullResourcePath);
			var baseName = Path.GetFileNameWithoutExtension(fullResourcePath);

			try {
				return Directory.GetFiles(path, baseName + "*.resx", SearchOption.TopDirectoryOnly);
			}
			catch {
				return new string[0];
			}
		}

		public static string ExtractLanguageFromFilename(string resourcePath) {
			int lastDot = resourcePath.LastIndexOf('.');
			int penultDot = resourcePath.Substring(0, lastDot).LastIndexOf('.');

			//Check invalid language codes (must be default resource without language specifier)
			if (lastDot - penultDot > 7)
				return string.Empty;

			return resourcePath.Substring(penultDot + 1, lastDot - penultDot - 1);
		}

		public static string AppendLanguageToBaseFilename(string resourcePath, string languageCode) {
			if (!string.IsNullOrEmpty(languageCode)) {
				var dotIndex = resourcePath.LastIndexOf('.');
				return resourcePath.Substring(0, dotIndex) + "." + languageCode + resourcePath.Substring(dotIndex);
			}
			else
				return resourcePath;
		}

	}
}
